﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using TfsBranchLib2.Model;
using TfsBranchLib2.View;

namespace MBas13.BranchedHistory2
{
    /// <summary>
    ///     This class implements the tool window exposed by this package and hosts a user control.
    ///     In Visual Studio tool windows are composed of a frame (implemented by the shell) and a pane,
    ///     usually implemented by the package implementer.
    ///     This class derives from the ToolWindowPane class provided from the MPF in order to use its
    ///     implementation of the IVsUIElementPane interface.
    /// </summary>
    [Guid("7f901839-4e8d-4f6b-bace-88e5c956613b")]
    public class BranchedHistoryWindow : ToolWindowPane
    {
        private BranchedHistoryControl control;

        /// <summary>
        ///     Standard constructor for the tool window.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public BranchedHistoryWindow() :
            base(null)
        {
            // Set the window title reading it from the resources.
            Caption = Resources.BranchedHistoryWindowTitle;
            // Set the image that will appear on the tab of the window frame
            // when docked with an other window
            // The resource ID correspond to the one defined in the resx file
            // while the Index is the offset in the bitmap strip. Each image in
            // the strip being 16x16.
            BitmapResourceID = 301;
            BitmapIndex = 1;

            InitContent();
        }

        private void InitContent()
        {
            try
            {
                // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
                // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on 
                // the object returned by the Content property.
                object content = null;
                //if (BranchedHistory2Package.Current != null)
                //{
                //    content = new BranchedHistoryControl();

                //}
                //else
                //{
                //    content = new BranchedHistoryControl();
                //}
                control = new BranchedHistoryControl();
                control.OnCompare += control_OnCompare;
                control.OnShowChangesetDetails += control_OnShowChangesetDetails;
                content = control;
                Content = content;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }

        private void control_OnShowChangesetDetails(object source, BranchedHistoryOutputControl.ChangesetDetailsEventArgs args)
        {
            var package = BranchedHistory2Package.Current;
            if (package != null)
            {
                package.ShowChangesetDetails(args.ChangesetId);
            }
        }

        private void control_OnCompare(object sender, BranchedHistoryOutputControl.CompareEventArgs e)
        {
            var package = BranchedHistory2Package.Current;
            if (package != null)
            {
                package.ShowCompare(e.VersionControlServer, e.isFile, e.Path1, e.Version1, e.Path2, e.Version2);
            }
        }
    }
}